package com.example.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admin.mapper.AdChannelMapper;
import com.example.admin.pojo.AddChannel;
import com.example.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.AnnotationUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * The type Ad channel service.
 */
@Service
public class AdChannelServiceImpl implements AdChannelService {
    /**
     * The Ad channel mapper.
     */
    @Autowired
    AdChannelMapper adChannelMapper;
    
    
    @Override
    public List<AdChannel> getAdChannelAll(ChannelDto channelDto) {
        QueryWrapper<AdChannel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",channelDto.getName());
        Page<AdChannel> adChannelPage = new Page<>(channelDto.getPage(),channelDto.getSize());
        IPage<AdChannel> adChannelIPage = adChannelMapper.selectPage(adChannelPage, queryWrapper);
        List<AdChannel> records = adChannelIPage.getRecords();
        return records;
    }
    
    @Override
    public ResponseResult deleteAdChannel(Integer id) {
        QueryWrapper<AdChannel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        AdChannel adChannel = adChannelMapper.selectOne(queryWrapper);
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        if (adChannel.getStatus() == 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }else {
            int byId = adChannelMapper.deleteById(id);
            return ResponseResult.okResult(byId,"删除成功");
        }
    }

    @Override
    public ResponseResult addAdChannel(AddChannel addChannel) {
        List<AdChannel> adChannels = adChannelMapper.selectList(null);
        for (AdChannel adChannel : adChannels) {
            if (adChannel.getName().equals(addChannel.getName())){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
        }
        AdChannel adChannel = new AdChannel();
        BeanUtils.copyProperties(addChannel,adChannel);
        adChannel.setIsDefault(0);
        adChannel.setCreatedTime(new Date());
        adChannel.setStatus(0);
        adChannelMapper.insert(adChannel);
        return ResponseResult.okResult("添加成功");
    }
    
    @Override
    public ResponseResult editAdChannel(AddChannel addChannel) {
        System.out.println(addChannel);
        QueryWrapper<AdChannel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",addChannel.getId());
        AdChannel channel = adChannelMapper.selectOne(queryWrapper);
        System.out.println(channel);
        List<AdChannel> adChannels = adChannelMapper.selectList(null);
        if (!addChannel.getName().equals(channel.getName())){
            for (AdChannel adChannel : adChannels) {
                System.out.println(adChannel.getName());
                if (adChannel.getName().equals(addChannel.getName())){
                    return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
                }
            }
        }
        AdChannel adChannel = new AdChannel();
        BeanUtils.copyProperties(addChannel,adChannel);
        adChannel.setStatus(1);
        adChannelMapper.updateById(adChannel);
        return ResponseResult.okResult("修改成功");
    }
}
